package entity

import (
	"github.com/jinzhu/gorm"
)

type Tag struct {
	ID      uint   `json:"id"`
	TagName string `json:"tag_name"`
	TagDes  string `json:"tag_des"`
}

func (t Tag) Create(db *gorm.DB) error {
	err := db.Create(&t).Error
	return err
}

func (t Tag) Count(db *gorm.DB) (uint, error) {
	var cnt uint
	err := db.Model(&t).Count(&cnt).Error
	return cnt, err
}

func (t Tag) GetByPagination(db *gorm.DB, offset, pageSize int) ([]Tag, error) {
	var list []Tag
	err := db.Limit(pageSize).Offset(offset).Order("id").Find(&list).Error
	if gorm.ErrRecordNotFound == err {
		return list, nil
	} else {
		return list, err
	}
}

func (t Tag) Update(db *gorm.DB) error {
	return db.Save(t).Error
}

func (t Tag) Delete(db *gorm.DB) error {
	return db.Delete(t).Error
}

func (t Tag) GetBySearchText(db *gorm.DB, searchText string) ([]Tag, error) {
	var list []Tag
	err := db.Where("tag_name like ?", "%"+searchText+"%").Find(&list).Error
	return list, err
}

func (t Tag) GetAll(db *gorm.DB) ([]Tag, error) {
	var list []Tag
	err := db.Find(&list).Error
	return list, err
}
